home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / HyperCard Related / XCMDs & XFCNs / ClipBoardToPictFile / ClipBoardToPictFile.p < prev    next >
Encoding:
Text File  |  1991-03-29  |  4.9 KB  |  193 lines  |  [TEXT/MPS ]

  1. {$R-}
  2. {$S ClipBoardToPictFile }
  3.  
  4.  
  5.     ClipBoardToPictFile(FileName, CreatorType)
  6.  
  7.      This HyperCard XFCN takes the PICT on the clipboard and turns it into
  8.     a PICT file with the given name.  It does not matter whether the PICT came
  9.     from the HyperCard paint tools, from another XCMD, or from another
  10.     application altogether.
  11.     
  12.     The optional parameter CreatorType is a four-character string
  13.     which will be the creator type of the file.  This will allow the file
  14.     to be double-clicked to invoke the corresponding application.  The 
  15.     default value is '????' -- which means no application.
  16.     
  17.     If it is successful, then empty is returned, otherwise the return value
  18.     is an error message.}
  19.  
  20. UNIT DummyUnit;
  21.  
  22. INTERFACE
  23.  
  24.     USES {* ToolIntf, PackIntf, *}
  25.             Menus, Events, TextEdit, HyperXCmd, 
  26.             OSIntf, Scrap, QuickDraw;
  27.  
  28.     PROCEDURE EntryPoint(paramPtr: XCmdPtr);
  29.  
  30. IMPLEMENTATION
  31.  
  32.     PROCEDURE ClipBoardToPictFile(paramPtr: XCmdPtr);
  33.     FORWARD;
  34.  
  35.     PROCEDURE EntryPoint(paramPtr: XCmdPtr);
  36.     BEGIN
  37.         ClipBoardToPictFile(paramPtr)
  38.     END { entrypoint } ;
  39.  
  40.  
  41.     PROCEDURE ClipBoardToPictFile(paramPtr: XCmdPtr);
  42.     
  43.     CONST
  44.     
  45.     MinParams =        1;
  46.     MaxParams =        2;
  47.  
  48.     PictHeaderSize = 512;
  49.     XFCNSignature = 'Created by ClipBoardToPictFile XFCN by Chris Thorman';
  50.     
  51.     TYPE
  52.     
  53.     ParamArray =        PACKED ARRAY [1..MaxParams] OF Str255;
  54.  
  55.     PictFileHeader     =    PACKED ARRAY [1..PictHeaderSize] OF CHAR;
  56.  
  57.  
  58.     VAR
  59.     
  60.     ParamStrings:            ParamArray;
  61.  
  62.     FileNameParam:            Str255;
  63.     CreatorTypeParam:        OSType;
  64.     
  65.     OutFileRefNum:            Integer;
  66.  
  67.     ThePict:                Handle;
  68.     PictSize:                LONGINT;
  69.     ScrapOffset:            LONGINT;
  70.     FileError:                Integer;
  71.     Success:                Boolean;
  72.     
  73.         PROCEDURE ExitWithMessage(aString:    Str255);
  74.         BEGIN
  75.             WITH paramPtr^ DO BEGIN
  76.                 returnValue := PasToZero(paramPtr, aString);
  77.                 EXIT(ClipBoardToPictFile);
  78.             END;
  79.         END;
  80.  
  81.         PROCEDURE ExitWithError(aString: Str255);
  82.         BEGIN
  83.             ExitWithMessage(concat('•••••••• Error: ', aString, '.'));
  84.         END;
  85.                         
  86.         FUNCTION WritePICTFile(ThePict: Handle; OutFileRefNum: Integer): Boolean;
  87.         VAR
  88.             Success:        Boolean;
  89.             FileHeader:        PictFileHeader; 
  90.             NumBytes:        LONGINT;
  91.             Signature:        Str255;
  92.             
  93.         BEGIN
  94.         
  95.             {* The file header contains nothing but a gratuitous signature *}
  96.             Signature :=    XFCNSignature;
  97.             
  98.             BlockMove(Ptr(ORD(@Signature) + 1), @FileHeader, length(Signature));
  99.             
  100.             NumBytes := PictHeaderSize;
  101.             Success := (FSWrite(OutFileRefNum, NumBytes, @FileHeader) = NoErr);
  102.             WritePICTFile := Success;
  103.             IF (NOT Success) THEN Exit(WritePICTFile);
  104.             
  105.             {* The rest of the file is just the PICT itself *}
  106.             NumBytes := GetHandleSize(ThePict);
  107.             Success := (FSWrite(OutFileRefNum, NumBytes, ThePict^) = NoErr);
  108.             WritePICTFile := Success;
  109.             IF (NOT Success) THEN Exit(WritePICTFile);
  110.         
  111.         END;
  112.             
  113.         PROCEDURE ParseParams;
  114.         VAR
  115.             ParamNum:            integer;
  116.         BEGIN
  117.             WITH paramPtr^ DO 
  118.             BEGIN
  119.                 IF (paramCount < MinParams) THEN ExitWithError('Too few parameters');
  120.                 IF (paramCount > MaxParams) THEN ExitWithError('Too many parameters');
  121.             
  122.                 ParamNum := 1; {* Required *}
  123.                 
  124.                 ZeroToPas(ParamPtr, Params[ParamNum]^, ParamStrings[ParamNum]);
  125.                 FileNameParam := ParamStrings[ParamNum];
  126.                 IF (FileNameParam = '') THEN ExitWithError('Empty file name');
  127.                 
  128.                 ParamNum := 2; {* Optional *}
  129.                 
  130.                 IF (paramCount >= ParamNum) THEN
  131.                     BEGIN
  132.                         ZeroToPas(paramPtr, params[ParamNum]^, ParamStrings[ParamNum]);  
  133.                         IF (length(ParamStrings[ParamNum]) <> 4)
  134.                         THEN ExitWithError(concat('Bad Creator Type: ', ParamStrings[ParamNum]));
  135.                         
  136.                         CreatorTypeParam[1] := ParamStrings[ParamNum][1];
  137.                         CreatorTypeParam[2] := ParamStrings[ParamNum][2];
  138.                         CreatorTypeParam[3] := ParamStrings[ParamNum][3];
  139.                         CreatorTypeParam[4] := ParamStrings[ParamNum][4];
  140.                     END
  141.                 ELSE
  142.                     BEGIN
  143.                         CreatorTypeParam := '????';
  144.                     END;    
  145.             END;
  146.         END;
  147.             
  148.     BEGIN {ClipBoardToPictFile}
  149.         
  150.         ParseParams;
  151.     
  152.         ThePict := NewHandle(0);
  153.         IF (ThePict = NIL) THEN ExitWithError('Couldn’t allocate zero-size handle');
  154.         
  155.         PictSize := GetScrap(ThePict, 'PICT', ScrapOffset);    
  156.         IF (PictSize = 0)
  157.         THEN 
  158.             BEGIN
  159.                 DisposHandle(ThePict);
  160.                 ExitWithError('Pict was of zero size');
  161.             END;
  162.         IF (PictSize < 0)
  163.         THEN 
  164.             BEGIN
  165.                 DisposHandle(ThePict);
  166.                 IF (PictSize = NoTypeErr) THEN ExitWithError('No data of type PICT on clipboard');
  167.                 ExitWithError('Unknown error in getting PICT');
  168.             END;
  169.     
  170.         IF (Create(FileNameParam, 0, CreatorTypeParam, 'PICT') <> NoErr)  
  171.         THEN ExitWithError(concat('Couldn’t create file: ', FileNameParam, '.  It may already exist'));
  172.  
  173.         IF (FSOpen(FileNameParam, 0, OutFileRefNum) <> NoErr)  
  174.         THEN ExitWithError(concat('Couldn’t open file: ', FileNameParam));
  175.  
  176.         Success := WritePICTFile(ThePict, OutFileRefNum);
  177.         
  178.         DisposHandle(ThePict);
  179.         FileError := FSClose(OutFileRefNum);
  180.         
  181.         IF (NOT Success) THEN ExitWithError(concat('Error Writing PICT to File: ', FileNameParam));
  182.         IF (FileError <> NoErr) THEN ExitWithError('Error closing file');
  183.         
  184.         ExitWithMessage('');
  185.         
  186.  
  187.     END { ClipBoardToPictFile } ;
  188.  
  189. END. { DummyUnit }
  190.  
  191.  
  192.